﻿using Service.Core.Repository.Core;
using Service.Framework.OperationLog;
using Service.Framework.Utils;
using SqlSugar;

namespace Service.Core.Repository.Service
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    public class OperationLogService(IRepository<OperationLog> repository) : IOperationLogService
    {
        public async Task<(List<OperationLog> list, int totalCount)> GetOperationLogs(int pageIndex, int pageSize, OperationLogType? type = null, string userId = "")
        {
            var exp = Expressionable.Create<OperationLog>()
                 .AndIF(type.HasValue, s => s.Type.Equals(type.Value))
                 .AndIF(!userId.IsNullOrWhiteSpace(), s => s.UserId.Equals(userId));

            RefAsync<int> totalCount = 0;
            var pages = await repository.Db.Queryable<OperationLog>()
                .Where(exp.ToExpression()).SplitTable(tabs => tabs.Take(3)).OrderByDescending(s => s.Id)
                .ToPageListAsync(pageIndex, pageSize, totalCount);
            return (pages, totalCount);
        }
    }
}
